package com.mmall.controller.backend;

import com.google.common.collect.Maps;
import com.mmall.commons.Const;
import com.mmall.commons.ResponseCode;
import com.mmall.commons.ServiceResponse;
import com.mmall.pojo.Product;
import com.mmall.pojo.User;
import com.mmall.service.IFileService;
import com.mmall.service.IProductService;
import com.mmall.service.IUserService;
import com.mmall.util.FTPUtil;
import com.mmall.util.PropertiesUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @auther Mr.m
 * @date 2019/3/20-17:35
 */
@Controller
@RequestMapping("manage/product")
public class ProductManageController {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private IProductService iProductService;
    @Autowired
    private IFileService iFileService;

    @RequestMapping("/save.do")
    @ResponseBody
    public ServiceResponse productSave(HttpSession session, Product product) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登入，请登入管理员");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充添加产品的业务逻辑
            return iProductService.saveOrUpdateProduct(product);

        } else {
            return ServiceResponse.createByErrorMessage("无权限操作");
        }


    }

    @RequestMapping("/set_sale_status.do")
    @ResponseBody
    public ServiceResponse setSaleStatus(HttpSession session, Integer productId, Integer status) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登入，请登入管理员");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充产品状态的业务逻辑
            return iProductService.setSaleStatus(productId, status);
        } else {
            return ServiceResponse.createByErrorMessage("无权限操作");
        }


    }

    @RequestMapping("/detail.do")
    @ResponseBody
    public ServiceResponse getDetail(HttpSession session, Integer productId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登入，请登入管理员");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充产品状态的业务逻辑
            return iProductService.manageProductDetail(productId);

        } else {
            return ServiceResponse.createByErrorMessage("无权限操作");
        }


    }

    @RequestMapping("/list.do")
    @ResponseBody
    public ServiceResponse getList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登入，请登入管理员");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充分页业务
            return iProductService.getProductList(pageNum, pageSize);

        } else {
            return ServiceResponse.createByErrorMessage("无权限操作");
        }
    }

    //搜索
    @RequestMapping("/search.do")
    @ResponseBody
    public ServiceResponse productSearch(HttpSession session, String productName, Integer productId, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登入，请登入管理员");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充搜索业务
            return iProductService.searchProduct(productName, productId, pageNum, pageSize);

        } else {
            return ServiceResponse.createByErrorMessage("无权限操作");
        }
    }

    @RequestMapping("/upload.do")
    @ResponseBody
    public ServiceResponse upload(HttpSession session, @RequestParam(value = "upload_file", required = false) MultipartFile file, HttpServletRequest request) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登入，请登入管理员");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充搜索业务
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = iFileService.upLoad(file, path);
            String url = PropertiesUtil.getProperties("ftp.server.http.prefix") + targetFileName;
            Map fileMap = new HashMap();
            fileMap.put("uri", targetFileName);
            fileMap.put("url", url);
            return ServiceResponse.createBySuccess(fileMap);
        } else {
            return ServiceResponse.createByErrorMessage("无权限操作");
        }

    }

    @RequestMapping("/richtext_img_upload.do")
    @ResponseBody
    public Map richtextImgUpload(HttpSession session, @RequestParam(value = "upload_file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = Maps.newHashMap();
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            resultMap.put("success", false);
            resultMap.put("msg", "请登入管理员");
            return resultMap;
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充搜索业务
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = iFileService.upLoad(file, path);
            if (StringUtils.isNotBlank(targetFileName)) {
                resultMap.put("success", false);
                resultMap.put("msg", "上传失败");
                return resultMap;
            }
            //富文本中对后端返回值是有要求的，我们使用时simditor的要求进行返回
            String url = PropertiesUtil.getProperties("ftp.server.http.prefix");
            resultMap.put("success", true);
            resultMap.put("msg", "上传成功");
            resultMap.put("file_path", url);
            //富文件上传需要设置响应头
            response.addHeader("Accses_Control_Allow_Headers", "x_File_Name");
            return resultMap;
        } else {
            resultMap.put("success", false);
            resultMap.put("msg", "无权限操作");
            return resultMap;
        }

    }
}
